package com.yun.baymax.activity;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yun.baymax.R;
import com.yun.baymax.bean.adapter.NoteInfo;
import com.yun.baymax.utils.ToastUtils;
import com.yun.lib.utils.L;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class NoteEditActivity extends BaseActivity implements View.OnClickListener {

    private TextView title;
    private TextView complete;
    private ImageView switchColor;
    private EditText content;
    private NoteInfo note;
    private AlertDialog alertDialog;
    private View editLay;
    private boolean isAdd;

    @Override
    protected int setLayoutResourceId() {
        return R.layout.activity_note_edit;
    }

    @Override
    protected void initVariables() {
        if (getIntent().getSerializableExtra("note") == null) {
            note = new NoteInfo(BmobUser.getCurrentUser().getUsername(), getResources().getColor(R.color.note_default));
            isAdd = true;
        } else
            note = (NoteInfo) getIntent().getSerializableExtra("note");
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        title = (TextView) findViewById(R.id.title);
        editLay = findViewById(R.id.note_edit);
        complete = (TextView) findViewById(R.id.complete);
        switchColor = (ImageView) findViewById(R.id.color_name);
        content = (EditText) findViewById(R.id.content);
        content.setText(note.getContent());
        editLay.setBackgroundColor(note.getColor());
        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dealData();
                finish();
            }
        });
        complete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dealData();
                finish();
            }
        });
        switchColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
    }

    private void dealData() {
        String content = NoteEditActivity.this.content.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            if (!isAdd)
                deleteUP(note);
        } else if (isAdd) {
            note.setContent(content);
            saveUP(note);
        } else if (!content.equals(note.getContent())) {
            note.setContent(content);
            updateUP(note);
        }
    }

    @Override
    protected void loadData() {
    }

    private void saveUP(NoteInfo noteInfo) {
        noteInfo.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (e != null)
                    ToastUtils.showShortToast(e.getMessage());
                else
                    L.i("添加成功");
            }
        });
    }

    private void updateUP(NoteInfo noteInfo) {
        noteInfo.update(noteInfo.getObjectId(), new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (null == e) {
                } else ToastUtils.showShortToast(e.getMessage());
            }
        });
    }

    private void deleteUP(NoteInfo data) {
        data.delete(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if (e == null)
                    L.i("删除成功");
                else ToastUtils.showShortToast(e.getMessage());
            }
        });
    }

    @Override
    public void onBackPressed() {
        dealData();
        super.onBackPressed();
    }

    @Override
    protected void onStop() {
        super.onStop();
        setResult(10);
    }

    private void showDialog() {
        View dialogView = getLayoutInflater().inflate(R.layout.dialog_choose_color, null);//(ViewGroup) findViewById(R.id.dialog)
        View one = dialogView.findViewById(R.id.one);
        View two = dialogView.findViewById(R.id.two);
        View three = dialogView.findViewById(R.id.three);
        View four = dialogView.findViewById(R.id.four);
        View five = dialogView.findViewById(R.id.five);
        View six = dialogView.findViewById(R.id.six);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setView(dialogView);
        alertDialog = builder.create();
        alertDialog.show();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.one:
                editLay.setBackgroundColor(getResources().getColor(R.color.note_default));
                note.setColor(getResources().getColor(R.color.note_default));
                break;
            case R.id.two:
                editLay.setBackgroundColor(getResources().getColor(R.color.note_gray));
                note.setColor(getResources().getColor(R.color.note_gray));
                break;
            case R.id.three:
                editLay.setBackgroundColor(getResources().getColor(R.color.note_blue));
                note.setColor(getResources().getColor(R.color.note_blue));
                break;
            case R.id.four:
                editLay.setBackgroundColor(getResources().getColor(R.color.note_green));
                note.setColor(getResources().getColor(R.color.note_green));
                break;
            case R.id.five:
                editLay.setBackgroundColor(getResources().getColor(R.color.note_red));
                note.setColor(getResources().getColor(R.color.note_red));
                break;
            case R.id.six:
                editLay.setBackgroundColor(getResources().getColor(R.color.note_yellow));
                note.setColor(getResources().getColor(R.color.note_yellow));
                break;
            default:
                break;
        }
        if (alertDialog.isShowing())
            alertDialog.dismiss();
    }
}
